package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.dto.req.DeviceAlarmThresholdReqDTO;
import com.ruoyi.system.domain.dto.resp.DeviceAlarmThresholdRespDTO;
import com.ruoyi.system.service.DeviceAlarmThresholdService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备阈值设定表(DeviceAlarmThreshold)表控制层
 *
 * @author panlei
 * @since 2025-02-27 15:46:57
 */
@RestController
@RequestMapping("/system/thresholdConfig")
@Api(tags = "运维管理")
public class DeviceAlarmThresholdController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private DeviceAlarmThresholdService deviceAlarmThresholdService;

    /**
     * 通过查询条件过滤阈值列表
     *
     * @param reqDTO 主键
     * @return 单条数据
     */
    @PostMapping("/list")
    @ApiOperation("通过查询条件过滤阈值列表")
    @PreAuthorize("@ss.hasPermi('system:thresholdconfig:list')")
    public TableDataInfo list(@RequestBody DeviceAlarmThresholdReqDTO reqDTO) {
        startPage();
        List<DeviceAlarmThresholdRespDTO> list = deviceAlarmThresholdService.list(reqDTO);
        return getDataTable(list);
    }

    /**
     * 新增数据
     *
     * @param reqDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("增加告警阈值")
    public AjaxResult add(@RequestBody DeviceAlarmThresholdReqDTO reqDTO) {
        this.deviceAlarmThresholdService.add(reqDTO);
        return success();
    }

    /**
     * 修改数据
     *
     * @param reqDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation("修改告警阈值")
    public AjaxResult update(@RequestBody DeviceAlarmThresholdReqDTO reqDTO) {
        this.deviceAlarmThresholdService.update(reqDTO);
        return success();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ApiOperation("删除告警阈值")
    public AjaxResult delete(@RequestBody List<String> idList) {
        return success(this.deviceAlarmThresholdService.delete(idList));
    }


    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
//    @PreAuthorize("@ss.hasPermi('system:thresholdconfig:list')")
//    @GetMapping("/list")
    public TableDataInfo list(SysConfig config)
    {
        startPage();
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:thresholdconfig:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfig config)
    {
        List<SysConfig> list = configService.selectConfigList(config);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:thresholdconfig:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable Long configId)
    {
        return success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey)
    {
        return success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:thresholdconfig:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysConfig config)
    {
        if (!configService.checkConfigKeyUnique(config))
        {
            return error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(getUsername());
        return toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:thresholdconfig:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysConfig config)
    {
        if (!configService.checkConfigKeyUnique(config))
        {
            return error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(getUsername());
        return toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:thresholdconfig:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        configService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("@ss.hasPermi('system:thresholdconfig:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        configService.resetConfigCache();
        return success();
    }
}

